<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.eccode" placeholder="eccode"></el-input>
				</el-form-item>
				<el-form-item>
					<el-input v-model="filters.ecname" placeholder="ec账号"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="findPagePoolConfig">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="configs" highlight-current-row v-loading="listLoading" border max-height="620" style="width: 100%;">
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="upshopinfo.user.username" label="供货商" width="100" show-overflow-tooltip  >
			</el-table-column>
			<el-table-column prop="eccode" label="EC编码" width="100"  show-overflow-tooltip  >
			</el-table-column>
			<el-table-column prop="ecname" label="用户名" width="100" show-overflow-tooltip   >
			</el-table-column>
			<el-table-column prop="ecprodNum" label="集团产品号码" width="100" show-overflow-tooltip  >
			</el-table-column>
			<el-table-column prop="city" label="地市" width="100" show-overflow-tooltip  >
			</el-table-column>
			<el-table-column prop="pinyin" label="地市简称" width="100" show-overflow-tooltip  >
			</el-table-column>
			<el-table-column prop="ecpasswrod" label="密码" width="100" show-overflow-tooltip  >
			</el-table-column>
			<el-table-column prop="prdList" :formatter="prdList" label="产品列表" show-overflow-tooltip  >
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-pagination
				@current-change="handleCurrentChange"
				@size-change="handleSizeChange"
				layout="total,sizes,prev, pager, next"
				:total="total">
		</el-pagination>
		<!--编辑界面-->
		<el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="供货商" prop="useruuid" >
					<el-select v-model="editForm.useruuid" placeholder="请选择供货商" >
						<el-option
								v-for="item in users"
								:key="item.user.useruuid"
								:label="item.user.username"
								:value="item.user.useruuid">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="EC编码" prop="eccode">
					<el-input v-model="editForm.eccode" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="用户名" prop="ecname">
					<el-input v-model="editForm.ecname" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="集团产品号码" prop="ecprodNum">
					<el-input v-model="editForm.ecprodNum" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="地市" prop="city">
					<el-input v-model="editForm.city" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="地市简称" prop="pinyin">
					<el-input v-model="editForm.pinyin" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="密码" prop="ecpasswrod">
					<el-input v-model="editForm.ecpasswrod" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="产品列表" prop="prdList">
					<el-input   type="textarea" :autosize="{ minRows: 2, maxRows: 10}"   v-model="editForm.prdList"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--新增界面-->
		<el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="供货商" prop="useruuid" >
					<el-select v-model="addForm.useruuid" placeholder="请选择供货商" >
						<el-option
								v-for="item in users"
								:key="item.user.useruuid"
								:label="item.user.username"
								:value="item.user.useruuid">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="EC编码" prop="eccode">
					<el-input v-model="addForm.eccode" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="用户名" prop="ecname">
					<el-input v-model="addForm.ecname" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="集团产品号码" prop="ecprodNum">
					<el-input v-model="addForm.ecprodNum" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="地市" prop="city">
					<el-input v-model="addForm.city" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="地市简称" prop="pinyin">
					<el-input v-model="addForm.pinyin" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="密码" prop="ecpasswrod">
					<el-input v-model="addForm.ecpasswrod" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="产品列表"  prop="prdList">
					<el-input   type="textarea" :autosize="{ minRows: 2, maxRows: 10}"  v-model="addForm.prdList"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { findPagePoolConfig ,addPool,updatePool,findAllUpShopInfo} from '../../api/api';

	export default {
		data() {
			return {
				filters: {
					eccode: '',
					ecname:''
				},
                start:0,
                pageSize:10,
                total:0,
                loading: false,
				listLoading: false,
                configs: [],
                users:[],
				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
                    eccode: [
                        { required: true, message: '请输入EC编码', trigger: 'blur' }
                    ],ecname: [
                        { required: true, message: '请输入用户名', trigger: 'blur' }
                    ],ecprodNum: [
                        { required: true, message: '请输入集团产品编号', trigger: 'blur' }
                    ],city: [
                        { required: true, message: '请输入地市', trigger: 'blur' }
                    ],pinyin: [
                        { required: true, message: '请输入地市简称,首字母大写', trigger: 'blur' }
                    ],ecpasswrod: [
                        { required: true, message: '请输入密码', trigger: 'blur' }
                    ],prdList: [
                        { required: true, message: '请输入产品列表', trigger: 'blur' }
                    ]
				},
				//编辑界面数据
				editForm: {
                    eccode: '',
                    ecname: '',
                    ecprodNum: '',
                    city:'',
                    pinyin:'',
                    ecpasswrod:'',
                    prdList:'',
                    useruuid:''
				},

				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
                    eccode: [
                        { required: true, message: '请输入EC编码', trigger: 'blur' }
                    ],ecname: [
                        { required: true, message: '请输入用户名', trigger: 'blur' }
                    ],ecprodNum: [
                        { required: true, message: '请输入集团产品编号', trigger: 'blur' }
                    ],city: [
                        { required: true, message: '请输入地市', trigger: 'blur' }
                    ],pinyin: [
                        { required: true, message: '请输入地市简称,首字母大写', trigger: 'blur' }
                    ],ecpasswrod: [
                        { required: true, message: '请输入密码', trigger: 'blur' }
                    ],prdList: [
                        { required: true, message: '请输入产品列表', trigger: 'blur' }
                    ]
				},
				//新增界面数据
				addForm: {
                    eccode: '',
					ecname: '',
					ecprodNum: '',
					city:'',
					pinyin:'',
					ecpasswrod:'',
					prdList:'',
                    useruuid:''
				}
			}
		},
		methods: {
            prdList:function (row, column) {
				return JSON.stringify(row.prdList);
            },
			handleCurrentChange:function(val){
                this.start = val - 1;
                this.findPagePoolConfig();
            },
            handleSizeChange:function(val){
                this.pageSize = val;
                this.findPagePoolConfig();
            },
            findPagePoolConfig() {
                let para = {
                    start:this.start,
                    pageSize:this.pageSize,
                    eccode:this.filters.eccode,
					ecname:this.filters.ecname
                };
                this.loading = true;
                findPagePoolConfig(para).then(datas => {
                    this.loading = false;
                    let { message, code, data } = datas;
                    if (code !== "0") {
                        this.$message({
                            message: message,
                            type: 'error'
                        });
                    } else {
                        console.log(data)
                        this.configs = data.result;
                        this.total = data.totalCount;
                    }
                }).catch(e =>{

                });
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
                let para = {
                    upprouuid:''
                };
                findAllUpShopInfo(para).then(datas => {
                    let { message, code, data } = datas;
                    if (code !== "0") {
                        this.$message({
                            message: message,
                            type: 'error'
                        });
                    } else {
                        this.users =   data;
                    }
                }).catch(e =>{
                    this.listLoading = false;
                });
				this.editForm.useruuid = row.upshopinfo.user.useruuid;
                this.editForm.prdList = JSON.stringify(row.prdList);
			},
			//显示新增界面
			handleAdd: function () {
				this.addFormVisible = true;
				this.addForm = {
                    eccode: '',
                    ecname: '',
                    ecprodNum: '',
                    city:'',
                    pinyin:'',
                    ecpasswrod:'',
                    prdList:''
				};
                let para = {
                    upprouuid:''
                };
                findAllUpShopInfo(para).then(datas => {
                    let { message, code, data } = datas;
                    if (code !== "0") {
                        this.$message({
                            message: message,
                            type: 'error'
                        });
                    } else {
                        this.users =   data;
                    }
                }).catch(e =>{
                    this.listLoading = false;
                });
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							let para = Object.assign({}, this.editForm);
                            updatePool(para).then((res) => {
                                this.editLoading = false;
                                let { message, code, data } = res;
                                if (code !== "0") {
                                    this.$message({
                                        message: message,
                                        type: 'error'
                                    });
                                } else {
                                    this.$message({
                                        message: '提交成功',
                                        type: 'success'
                                    });
                                    this.$refs['editForm'].resetFields();
                                    this.editFormVisible = false;
                                    this.findPagePoolConfig();
                                }
							});
						});
					}
				});
			},
			//新增
			addSubmit: function () {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							let para = Object.assign({}, this.addForm);
                            addPool(para).then((res) => {
                                this.addLoading = false;
                                let { message, code, data } = res;
                                if (code !== "0") {
                                    this.$message({
                                        message: message,
                                        type: 'error'
                                    });
                                } else {
                                    this.$message({
                                        message: '提交成功',
                                        type: 'success'
                                    });
                                    this.$refs['addForm'].resetFields();
                                    this.addFormVisible = false;
                                    this.findPagePoolConfig();
                                }
							});
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			}

		},
		mounted() {
			this.findPagePoolConfig();
		}
	}

</script>

<style scoped>

</style>
